/**
 * handle gateway device connect to server.
 */

var dispatcher = require("../Dev/dispatcher.js");
var cmdCodes = require("../CmdCode/cmdCode.js");

function usher(c) {
	"use strict";
	dispatcher(c, function(command, c1){
		clearTimeout(c1.usherTID);
		if (command.cmd === cmdCodes.CMD_GATEWAY_REGISTER) {
			dispatcher.defaultDataHandler(command, c1);
		} else {
			console.error("dev usher:close socket cause cmd not CMD_GATEWAY_REGISTER");
			c1.destroy();
		}
	});
	
	function timeout() {
		console.error("dev usher:close socket cause time out");
		c.destroy();
	}
	
	c.usherTID = setTimeout(timeout, 3*1000);
}

function handleConnect(c) {
	usher(c);
}

module.exports = handleConnect;
